﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Text;
using WinnerCMS.Common;
using System.Linq;
using WinnerCMS.Data;
using WinnerCMS.Model;
using WinnerCMS.BLL.PageFactory.TagModule;

#endregion

namespace WinnerCMS.BLL.SitemapBuilder
{
    public class SitemapBuilder
    {
        #region 建立Google地图

        /// <summary>
        /// 建立Google地图
        /// </summary>
        /// <param name="top"></param>
        /// <param name="changefreq"></param>
        /// <param name="priority"></param>
        /// <param name="sitePath"></param>
        /// <param name="fileName"></param>
        public void BuildingGoogle(int top, string changefreq, string priority, string sitePath, string fileName)
        {
            var list = new CategoryDal().GetChildren(0);
            StringBuilder xml = new StringBuilder();
            string tempString = ResHelper.GetResourceContents("WinnerCMS.BLL", "WinnerCMS.BLL.SitemapBuilder.Google.XML");
            string strItemTemplate = TagEngine.GetInnerModule(tempString, "ItemTemplate", GetWhich.ItemTemplate);

            foreach (var model in list.Where(x => x.LinkUrl.Length == 0))
            {
                var cList = new CoreManage().Latests(top, model.Id);
                int X = 0;
                foreach (Core cmodel in cList)
                {
                    if (X >= 50)
                    {
                        break;
                    }
                    xml.Append(strItemTemplate);

                    xml.Replace("@loc@", Link(cmodel.Link));
                    xml.Replace("@lastmod@", cmodel.UpdateTime.ToString());
                    xml.Replace("@changefreq@", changefreq);
                    xml.Replace("@priority@", priority);
                }
            }

            tempString = tempString.Replace(strItemTemplate, xml.ToString());

            new FileIO().WriterFile(sitePath + @"\" + fileName + ".xml", tempString);
        }

        #endregion

        public void BuildingBaidu(int top, string updatePeri, string sitePath, string fileName)
        {
            var list = new CategoryDal().GetChildren(0);
            StringBuilder XML = new StringBuilder();
            string TempString = ResHelper.GetResourceContents("WinnerCMS.BLL", "WinnerCMS.BLL.SitemapBuilder.Baidu.XML");
            string strItemTemplate = TagEngine.GetInnerModule(TempString, "ItemTemplate", GetWhich.ItemTemplate);

            foreach (var model in list.Where(x => x.LinkUrl.Length == 0))
            {
                var cList = new CoreManage().Latests(top, 0);
                int X = 0;
                foreach (dynamic cmodel in cList)
                {
                    if (X >= 100)
                    {
                        break;
                    }
                    XML.Append(strItemTemplate);

                    XML.Replace("@ContentTitle@", cmodel.ContentTitle);
                    XML.Replace("@link@", Link(cmodel.Link));
                    XML.Replace("@description@", "");
                    XML.Replace("@text@", Format(HTML.NoHtml(cmodel.Content).Replace("\r\n", "")));
                    XML.Replace("@image@", "");
                    XML.Replace("@author@", "");
                    XML.Replace("@source@", "");
                    XML.Replace("@pubDate@", cmodel.UpdateTime.ToString());
                }
            }

            TempString = TempString.Replace(strItemTemplate, XML.ToString());
            TempString = TempString.Replace("@updatePeri@", updatePeri);
            TempString = TempString.Replace("@webSite@", WebConfig.DomainName);

            new FileIO().WriterFile(sitePath + @"\" + fileName + ".xml", TempString);
        }


        private static string Link(string url)
        {
            if (url.StartsWith("http:", StringComparison.CurrentCultureIgnoreCase) == false)
            {
                return WebConfig.DomainName + url;
            }
            return url;
        }

        /// <summary>
        /// 格式化ＸＭＬ禁用符号
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private static string Format(string input)
        {
            StringBuilder outPut = new StringBuilder();
            outPut.Append(input);
            outPut.Replace("<", "&lt;");
            outPut.Replace(">", "&gt");
            outPut.Replace("&", "&amp;");
            outPut.Replace("\"", "&quot;");
            outPut.Replace("'", "&apos;");
            outPut.Replace(" ", "#x20");

            return outPut.ToString();
        }
    }
}